package com.block.channel.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.channel.service.IChannelCountInfoService;
import com.block.channel.service.IChannelCountUvInfoService;
import com.block.common.bean.CommonParam;
import com.block.common.enums.ChargeTypeEnum;
import com.block.common.model.dto.CountInfoDTO;
import com.block.common.service.IChannelCommonService;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.ChannelCountInfo;
import com.block.db.mapper.ChannelCountInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 渠道统计信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-09-01
 */
@Service
public class ChannelCountInfoServiceImpl extends ServiceImpl<ChannelCountInfoMapper, ChannelCountInfo> implements IChannelCountInfoService {

    @Autowired
    IChannelCommonService channelCommonService;
    @Autowired
    IChannelCountUvInfoService channelCountUvInfoService;

    @Async
    @Override
    public void countChannelUvDataInfo(ChannelBaseInfo channelBaseInfo, String reqIp, String userAgent, String randomId) {

        CountInfoDTO countInfoDTO = new CountInfoDTO(channelBaseInfo.getChannelCode());
        countInfoDTO.setUvCount(1);
        // 计算uv和uv成本
        if (ObjectUtil.equals(channelBaseInfo.getChargeType(), ChargeTypeEnum.UV.getValue())){
            countInfoDTO.setUvCost(channelBaseInfo.getPriceAmount());
        }

        try {

            UserAgent userAgentObject = UserAgentUtil.parse(userAgent);
            String browserName = userAgentObject.getBrowser().getName();

            if (StrUtil.equals(browserName, "MicroMessenger")){
                countInfoDTO.setRequestWechatCount(1);
            }

            if (StrUtil.equals(browserName, "QQBrowser")){
                countInfoDTO.setRequestQqCount(1);
            }

        }catch (Exception exception){
            exception.printStackTrace();
        }

        channelCommonService.updateChannelCount(countInfoDTO);
        // 每个有效uv的具体信息
        channelCountUvInfoService.saveUvCountInfo(channelBaseInfo, randomId, reqIp, userAgent);
    }

    /**
     * 统计登陆，注册信息
     * @param channelCode
     * @param isNew
     * @param commonParam
     */
    @Async
    @Override
    public void countLoginRegDataInfo(String channelCode, boolean isNew, CommonParam commonParam){

        ChannelBaseInfo channelBaseInfo = channelCommonService.getBaseInfoByCode(channelCode);

        CountInfoDTO countInfoDTO = new CountInfoDTO(commonParam.getChannelCode());
        countInfoDTO.setLoginCount(1);

        String dateInfo = countInfoDTO.getDateInfo();

        if (isNew){
            // 注册量
            countInfoDTO.setRegisterCount(1);
            countInfoDTO.setChargeType(ChargeTypeEnum.REG.getValue());

        }
        channelCommonService.updateChannelCount(countInfoDTO);
    }


}
